<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Log;

class WeChatController extends Controller
{
    public function redirect()
    {
        $app = app('wechat.official_account');

        $response = $app->oauth->scopes(['snsapi_userinfo'])
            ->redirect('/wechat');
        return $response;
    }

    public function callback()
    {
        $app = app('wechat.official_account');

        $user = $app->oauth->user();
        return $user;
    }
    /**
     * 处理微信的请求消息
     *
     * @return string
     */
    public function serve()
    {

        Log::info('request arrived.'); # 注意：Log 为 Laravel 组件，所以它记的日志去 Laravel 日志看，而不是 EasyWeChat 日志

        $app = app('wechat.official_account');
        $user = $app->oauth->user();
        return $user;

        $app->server->push(function($message) use ($app){
            //return $app->oauth->user();
            return "测试账号==欢迎关注 ！";
        Log::info('request arrived.'); # 注意：Log 为 Laravel 组件，所以它记的日志去 Laravel 日志看，而不是 EasyWeChat 日志

        $app = app('wechat.official_account');
        $app->server->push(function($message){
            return "欢迎关注 overtrue！";
        });

        return $app->server->serve();
    }

    public function autoRegister()
    {
        $app = app('wechat.official_account');

        $oauth = $app->oauth;

        $user = $oauth->user();
        return $user;
        return 111;
        $user = session('wechat.oauth_user.default');
        return $user;
    }
}
